package com.wcfsy.wechat.wx.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wcfsy.wechat.wx.entity.ReplyMessage;
import com.wcfsy.wechat.wx.pagination.IPage;
import com.wcfsy.wechat.wx.pagination.impl.Page;
import com.wcfsy.wechat.wx.sys.dao.WechatReplyMessageDao;
import com.wcfsy.wechat.wx.util.DateStyle;
import com.wcfsy.wechat.wx.util.DateUtil;
import com.wcfsy.wechat.wx.util.JsonMessage;
import com.wcfsy.wechat.wx.util.WechatUtil;

/**
 *
 * 项目名称：xn-wechat-activity
 * 
 * 类名称：WeChatUserController.java
 * 
 * 类描述：获取微信用户信息
 * 
 * 创建人： Rod Zhong
 * 
 * 创建时间：2015年8月13日 上午10:06:55
 * 
 * Copyright (c) 深圳市小牛科技有限公司-版权所有
 */
@Controller
@RequestMapping("/cms/wechatmsg/")
public class WeChatMsgController {

	Logger logger = LoggerFactory.getLogger(this.getClass());

	@Resource
	private WechatReplyMessageDao replyMessageDao;

	@Resource
	private WechatUtil wechatUtil;

	@RequestMapping("add/page")
	public String preAdd() {
		return "wechatmsg/message_text_add";
	}

	@RequestMapping("add")
	@ResponseBody
	public JsonMessage add(ReplyMessage replyMessage) {
		try {
			replyMessage.setCreateTime(DateUtil.DateToString(new Date(),
					DateStyle.YYYY_MM_DD_HH_MM_SS));
			replyMessageDao.add(replyMessage);
			return JsonMessage.getSuccessJson("添加成功");
		} catch (Exception e) {
			return JsonMessage.getErrorJson("消息配置保存失败");
		}
	}

	@RequestMapping("view/page")
	public String page() {
		return "wechatmsg/replyMessage_list";
	}

	@RequestMapping("view")
	@ResponseBody
	public IPage<ReplyMessage> view(int page, int rows) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("index", page);
		paramMap.put("size", rows);
		paramMap.put("limit", "limit");
		return new Page<ReplyMessage>(replyMessageDao.findByParam(paramMap),
				replyMessageDao.count(paramMap), page, rows);
	}

	@RequestMapping("delete")
	@ResponseBody
	public JsonMessage delete(int id) {
		try {
			replyMessageDao.deleteById(id);
			return JsonMessage.getSuccessJson("删除成功");
		} catch (Exception e) {
			return JsonMessage.getSuccessJson("删除失败");
		}
	}

	@RequestMapping("modify/page")
	public String preModify(int id, ModelMap modelMap) {
		modelMap.put("replyMessage", replyMessageDao.getById(id));
		return "wechatmsg/message_text_modify";
	}

	@RequestMapping("modify")
	@ResponseBody
	public JsonMessage modify(ReplyMessage replyMessage) {
		try {
			replyMessageDao.update(replyMessage);
			return JsonMessage.getSuccessJson("修改成功");
		} catch (Exception e) {
			return JsonMessage.getErrorJson("修改失败");
		}
	}

}
